# -*- coding: utf-8 -*-
import sip
sip.setapi('QString', 2)

from PyQt4 import QtGui, QtCore, uic
import sys, functools
import parser
import analytics
reload(parser)
reload(analytics)

CHAMPS = ['evelynn', 'zed', 'nunu', 'swain', 'taric', 'nasus', 'graves', 'nocturne', 'shaco', 'leesin', 'draven', 'nidalee', 'kassadin', 'sona', 'monkeyking', 'cassiopeia', 'skarner', 'malzahar', 'amumu', 'khazix', 'olaf', 'sivir', 'riven', 'nami', 'kogmaw', 'gangplank', 'syndra', 'poppy', 'volibear', 'kennen', 'ryze', 'gragas', 'garen', 'diana', 'karthus', 'jayce', 'sion', 'elise', 'alistar', 'vayne', 'ezreal', 'fiddlesticks', 'lux', 'fiora', 'twistedfate', 'anivia', 'drmundo', 'tryndamere', 'hecarim', 'darius', 'heimerdinger', 'jax', 'jarvaniv', 'ashe', 'malphite', 'irelia', 'annie', 'twitch', 'kayle', 'janna', 'blitzcrank', 'talon', 'leblanc', 'xinzhao', 'katarina', 'chogath', 'shen', 'warwick', 'fizz', 'missfortune', 'renekton', 'viktor', 'brand', 'corki', 'maokai', 'yorick', 'urgot', 'tristana', 'galio', 'vladimir', 'orianna', 'masteryi', 'soraka', 'varus', 'caitlyn', 'veigar', 'rengar', 'nautilus', 'lulu', 'pantheon', 'trundle', 'ahri', 'shyvana', 'udyr', 'zyra', 'mordekaiser', 'zilean', 'rammus', 'rumble', 'morgana', 'wukong', 'sejuani', 'xerath', 'ziggs', 'singed', 'karma', 'akali', 'leona', 'teemo']

class TeamToolGui(QtGui.QMainWindow):
	def __init__(self):
		super(QtGui.QMainWindow, self).__init__()
		self.cwidget = QtGui.QWidget()
		self.setCentralWidget(self.cwidget)
		self.layout = QtGui.QVBoxLayout(self)
		self.teaminputlayout = QtGui.QHBoxLayout();self.layout.addLayout(self.teaminputlayout)
		self.completer = QtGui.QCompleter(CHAMPS, self)
		self.cwidget.setLayout(self.layout)
		self.allie_name_le = []
		self.allie_id_le = []
		self.allie_champ_le = []
		self.allies_v_layout = QtGui.QVBoxLayout();self.teaminputlayout.addLayout(self.allies_v_layout)

		for i in range(5):
			_hl = QtGui.QHBoxLayout(self);self.allies_v_layout.addLayout(_hl)

			_input_name = QtGui.QLineEdit(self.cwidget);_hl.addWidget(_input_name)
			self.allie_name_le.append(_input_name)
			_input_name.returnPressed.connect(functools.partial(self.get_id, i))

			_input_id = QtGui.QLineEdit(self.cwidget);_hl.addWidget(_input_id)
			self.allie_id_le.append(_input_id)

			_input_champ = QtGui.QLineEdit(self.cwidget);_hl.addWidget(_input_champ)
			_input_champ.setCompleter(self.completer)
			self.allie_champ_le.append(_input_champ)

		self.vslabel = QtGui.QLabel("vs", self);self.teaminputlayout.addWidget(self.vslabel)

		self.enemy_champ_le = []
		self.enemy_v_layout = QtGui.QVBoxLayout();self.teaminputlayout.addLayout(self.enemy_v_layout)
		for i in range(5):
			_hl = QtGui.QHBoxLayout(self);self.enemy_v_layout.addLayout(_hl)

			_input_champ = QtGui.QLineEdit(self.cwidget);_hl.addWidget(_input_champ)
			_input_champ.setCompleter(self.completer)
			self.enemy_champ_le.append(_input_champ)

		self.analyze_btn = QtGui.QPushButton("go"); self.layout.addWidget(self.analyze_btn)
		self.analyze_btn.clicked.connect(self.analyze_btn_clicked)

		#char_set = unicode('"¡", "¿", "Á", "á", "À", "à", "Å", "å", "Ä", "ä", "Æ", "æ", "Ç", "ç", "É", "é", "È", "è", "Í", "í", "Ì", "ì", "Î", "î", "Ñ", "ñ", "Ó", "ó", "Ò", "ò", "Ô", "ô", "Ö", "ö", "Ø", "ø", "Ú", "ú", "Ù", "ù", "Ü", "ü", "Ž", "ž", "ß", "¥", "€", "£"', "utf-8")
		#self.unicodes_le = QtGui.QTextEdit(char_set, self);self.layout.addWidget(self.unicodes_le)

		self.previous_match_btn = QtGui.QPushButton(self);self.layout.addWidget(self.previous_match_btn)
		self.previous_match_btn.clicked.connect(self.analyze_previous_match)
	def analyze_previous_match(self):
		analytics.historic_analysis()

	def get_id(self, *args):
		i = args[0]
		name = self.allie_name_le[i].text().replace(" ", "+")
		id = parser.get_id_from_name(name, False)
		print name, i
		self.allie_id_le[i].setText(id)

	def analyze_btn_clicked(self):
		players = []
		e_champs = []
		for i in range(5):
			id = self.allie_id_le[i].text().strip()
			champ = self.allie_champ_le[i].text().strip()
			e_champ = self.enemy_champ_le[i].text().strip()
			if not id:
				id = None
			if not champ:
				champ = None
			players.append((champ, id))
			if e_champ:
				e_champs.append(e_champ)
		analytics.team_predict(players, e_champs)
		#print players, e_champs

def main():
	app = QtGui.QApplication(sys.argv)
	global lolgui
	try: lolgui.close()
	except: pass
	lolgui = TeamToolGui()
	lolgui.show()
	sys.exit(app.exec_())

main()

